﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Admin.Application;
using Windows.Admin.Domain;
using Windows.Infrastructure.Share.Security;

namespace Windows.Admin.Application
{
    public class SystemService : ISystemService
    {
        private readonly IUserService _userSvc;
        private readonly IAppService _appSvc;
        private readonly IPrivilegeService _privilegeSvc;
        private readonly ICurrentUser _currentUser;
        public SystemService(IPrivilegeService privilegeSvc, IAppService appSvc, IUserService userSvc, ICurrentUser currentUser)
        {
            _privilegeSvc = privilegeSvc;
            _appSvc = appSvc;
            _userSvc = userSvc;
            _currentUser = currentUser;
        }
        /// <summary>
        /// 初始化系统
        /// </summary>
        /// <returns></returns>
        public async Task<SystemResponse> Init()
        {
            var privileges = await _privilegeSvc.GetCurrentUserPrivileges();
            var appIds = privileges.Where(x => x.Access == AccessEnum.App.ToString()).Select(s=>s.AccessValue).ToList();
            List<AppTreeResponse> apps;
            if (_currentUser.IsAdmin)
                apps = await _appSvc.GetNoResourceApps();
            else
                apps = await _appSvc.GetNoResourceAppsByIds(appIds);

            SystemResponse system = new()
            {
                UserInfo = await _userSvc.CurrentUser(),
                Apps = apps,
                Shortcuts = await _appSvc.GetCurrentUserShortuts(),
                Licenses = await _privilegeSvc.GetCurrentUserLicenses()
            };
            return system;
        }
    }
}
